package admin.user;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CommonUtil;
import com.common.Dateutil;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import beans.subject.ChapterModelSearch;
import beans.subject.subchapterBean;
import beans.sys.UserBean;
import beans.user.UserSearch;
import beans.user.order;
import beans.user.ordersearch;
import beans.user.user;
import beans.user.withdrawcash;
import service.user.OrderServiceImpl;
import service.user.WXUserServiceImpl;
@Controller
@RequestMapping("/wxuserorder")
public class WXUserOrderController  extends BaseController{
	@Resource
	private WXUserServiceImpl<user> wxUserServiceImpl;
	@Resource
	private OrderServiceImpl<order> orderServiceImpl;

	@AuthPassport(actionName="index",controllerName="wxuserorder")
	@RequestMapping("/index")
	public String Index(){
		return "admin/wxuserorder/index";
	}
	
	@RequestMapping("/GetData")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="wxuserorder")
	public void DataGrid(ordersearch model){
		ResultData<List<order>> rd = new ResultData<>();
		try{
			rd.setPage(model.getPage());

			if(!CommonUtil.isNullObject(model.getEndtime())){
				model.setEndtime(Dateutil.nextDate(model.getEndtime()));
			}

			List<order> m = orderServiceImpl.queryByList(model);
			String[] ids = new String[m.size()];
			int i=0;
			for(order with : m) {
				ids[i] = with.getUid();
				i++;
			}
			if(ids.length>0) {
				List<user> userList = wxUserServiceImpl.queryByIds(ids);
				for(order with : m) {
					for(user tem : userList) {
						if(with.getUid().equals(tem.getOpenid())) {
							with.setNickname(tem.getNickname());
							break;
						}
					}
				}
			}
			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}
	
	@AuthPassport(actionName="index",controllerName="wxuserorder")
	@RequestMapping("/delete")
	@ResponseBody
	public void Delete() {
		ResultData<String> rd = new ResultData<>();
		String[] ids = getReq().getParameterValues("ids");
		try{
			orderServiceImpl.deletes(ids);
			rd.setType(1);
			rd.setMessage("删除成功");
		}
		catch(Exception e){
			e.printStackTrace();
			rd.setType(3);
			rd.setMessage("删除失败");
		}
		responseJson(rd);
	}
}
